package com.yuandian.customer.perception.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.yuandian.api.customer_perception.po.BasePortraitParams;
import com.yuandian.api.customer_perception.vo.MatterProcessCountTop5VO;
import com.yuandian.api.customer_perception.vo.MenuVisitCountTop5;
import com.yuandian.api.customer_perception.vo.OperatorActiveTrendVo;
import com.yuandian.api.customer_perception.vo.TimeConsumingTop5;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.mapper.UserPortraitMapper;
import com.yuandian.customer.perception.service.IUserPortraitService;
import com.yuandian.enums.TableNameEnum;
import com.yuandian.po.MergeDataConfig;
import com.yuandian.po.MergeField;
import com.yuandian.po.MultipleTableInfo;
import com.yuandian.utils.DynamicTableNameUtil;
import com.yuandian.utils.MergeDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author liXin
 * @description 单位画像
 * @date 2022/4/24
 */
@Service
public class UserPortraitServiceImpl implements IUserPortraitService {

    @Autowired
	UserPortraitMapper userPortraitMapper;

    /**
     * 操作员菜单访问量排行(TOP5)
     */
    @Override
    public R<Object> queryUserOperatorMenuVisitTop5(BasePortraitParams params) {
        if (StringUtils.isBlank(params.getIsAsc())){
            params.setIsAsc("asc");
        }
        List<MenuVisitCountTop5> menuVisitList = userPortraitMapper.queryMenuVisitCountTop5(params);
        return R.ok(menuVisitList);
    }

    /**
     * 操作员事项受理量排行(TOP5)
     */
    @Override
    public R<Object> queryUserOperatorProcessCountTop5(BasePortraitParams params) {
        if (StringUtils.isBlank(params.getIsAsc())) { params.setIsAsc("asc"); }
        String itemChangeTable = TableNameEnum.BUSI_ITEM_CHANGE.getName();
		List<MatterProcessCountTop5VO> visitCountList = userPortraitMapper.queryMattertatisticsTop5(params, itemChangeTable);
        return R.ok(visitCountList);
    }

    /**
     * 事项平均空闲时长排行(TOP5)
     */
    @Override
    public R<Object> queryMatterAvgFreeTimeTop5(BasePortraitParams params) {
        HashMap<String, List<TimeConsumingTop5>> map = new HashMap<>();
        String itemChangeTable = TableNameEnum.BUSI_ITEM_CHANGE.getName();

		List<TimeConsumingTop5> longTimeMatter = userPortraitMapper.queryMatterByLongestFreeTimeTop5(params, itemChangeTable);
		List<TimeConsumingTop5> shortTimeMatter = userPortraitMapper.queryMatterByShortestFreeTimeTop5(params, itemChangeTable);

        map.put("longTimeMatter", longTimeMatter);
        map.put("shortTimeMatter", shortTimeMatter);
        return R.ok(map);
    }

    /**
     * 事项平均办理时长排行(TOP5)
     */
    @Override
    public R<Object> queryMatterAvgProcessTimeTop5(BasePortraitParams params) {
        HashMap<String, List<TimeConsumingTop5>> map = new HashMap<>();
        String itemChangeTable = TableNameEnum.BUSI_ITEM_CHANGE.getName();

		List<TimeConsumingTop5> longTimeMatter = userPortraitMapper.queryMatterByLongestProcessTimeTop5(params, itemChangeTable);
		List<TimeConsumingTop5> shortTimeMatter = userPortraitMapper.queryMatterByShortestProcessTimeTop5(params, itemChangeTable);

		map.put("longProcessTimeMatter", longTimeMatter);
        map.put("shortProcessTimeMatter", shortTimeMatter);
        return R.ok(map);
    }


    /**
     * 访问量，办理时长趋势图
     */
    @Override
    public R<Object> queryVisitAndDealTimeTrend(BasePortraitParams params) {
		// 1. 获取表信息集
		MultipleTableInfo info = DynamicTableNameUtil.generalSingleTableNamesFromTimeRange(params.getStartTime(), params.getEndTime());
		List<String> tableNameList = Collections.singletonList(TableNameEnum.BUSI_STEP_AFFAIR_STATIST.getName());

		// 2. 设置表名、时间范围矫正
		params.setTableNameList(tableNameList);

        // 3、设置表名、时间范围矫正
        params.setTableNameList(tableNameList);

        List<OperatorActiveTrendVo> res=userPortraitMapper.queryVisitAndDealTimeTrend(params);
        // 5、归并数据
        Integer minScale = DynamicTableNameUtil.getMinScaleByTableType(info.getTableType());
        Integer scale = null;
        if (params.getScale() != null) {
            scale = DynamicTableNameUtil.getScaleByMinScaleAndParam(minScale, params.getScale());
        }

        List<MergeField> mergeFieldList = new ArrayList<>();
        mergeFieldList.add(new MergeField("reqCount"));
        mergeFieldList.add(new MergeField("dealTime"));

        Map<String, Object> map = MergeDataUtil.mergeData(new MergeDataConfig.Builder(
                OperatorActiveTrendVo.class,
                res,
                params.getStartTime(),
                params.getEndTime(),
                mergeFieldList)
                .cycleTimeKey("cycleTime")
                .labelTimeFormatter("MM-dd'\n'HH:mm")
                .scale(scale)
                .build());
        map.put("minScale", minScale);

        Map<String, List<Double>> resFieldMap = (Map<String, List<Double>>) map.get("resFieldMap");
        List<Double> totalDealTimeList = resFieldMap.get("dealTimeList");
        List<Double> reqCountList = resFieldMap.get("reqCountList");
        List<Double> list = new ArrayList<>();
        for (int i = 0; i < totalDealTimeList.size(); i++) {
            if (totalDealTimeList.get(i)!=0){
                double avgDealTime = totalDealTimeList.get(i)/reqCountList.get(i);
                list.add(Double.valueOf(String.format("%.2f", avgDealTime)));
            }else{
                list.add(0D);
            }

        }
        resFieldMap.put("avgDealTimeList", list);
        return R.ok(map);
    }

}
